﻿/**
 * @file setting_tab.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_LAUNCHER2 != 0

/*********************
 *      DEFINES
 *********************/
#define LV_COLOR_CHECKED LV_COLOR_MAKE(0x27, 0xB3, 0xF1)
/**********************
 *      TYPEDEFS
 **********************/
typedef struct {
    lv_watch_obj_ext_t oldext;
    lv_obj_t * list;
    lv_obj_t * add_list;
    uint8_t checked_num;
} lv_setting_tab_ext_t;

/**********************
 *  STATIC PROTOTYPES
 **********************/
static lv_obj_t * setting_tab_create(lv_obj_t * activity_obj);
static lv_obj_t * setting_tab_add_create(lv_obj_t * activity_obj);
static void setting_tab_prepare_destory(lv_obj_t * activity_obj);
static void setting_tab_add_prepare_destory(lv_obj_t * activity_obj);
static lv_obj_t * setting_tab_list_add(lv_obj_t * list, uint8_t tab_type, uint8_t choose);
static void setting_tab_add_event_cb(lv_obj_t * obj, lv_event_t e);
static void setting_tab_add_list(lv_obj_t * tab_list);
static lv_obj_t * setting_tab_list_get_btn(lv_obj_t * list, uint8_t index);
static lv_obj_t * setting_tab_create_title(lv_obj_t * par, uint16_t txtId);
static lv_setting_tab_ext_t * setting_tab_get_ext(void);
static lv_obj_t * setting_tab_create_list(lv_obj_t * par);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
void setting_tab_create_btn_action(lv_obj_t * btn, lv_event_t event)
{
    (void)btn;

    if(LV_EVENT_CLICKED != event) return;

    lv_watch_png_cache_all_free();

    lv_obj_t * obj = setting_tab_create(NULL);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static lv_setting_tab_ext_t * setting_tab_get_ext(void)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_SETTING_TAB);
    if(activity_obj == NULL) return NULL;

    lv_obj_t * watch_obj;
    lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    if(watch_obj == NULL) return NULL;

    if(watch_obj) {
        lv_setting_tab_ext_t * ext = lv_obj_get_ext_attr(watch_obj);
        return ext;
    }
    return NULL;
}

static void setting_tab_prepare_destory(lv_obj_t * activity_obj)
{
    printf("%s\n", __FUNCTION__);
    lv_watch_png_cache_all_free();
    lv_setting_tab_ext_t * ext = setting_tab_get_ext();
    ext->list = NULL;
    ext->checked_num = 0;
    launcher_tab_save();
    launcher_recreate();
}

static void setting_tab_add_prepare_destory(lv_obj_t * activity_obj)
{
    lv_setting_tab_ext_t * ext = setting_tab_get_ext();
    ext->checked_num = 0;
    ext->add_list = NULL;
}

static lv_obj_t * setting_tab_create_title(lv_obj_t * par, uint16_t txtId)
{
    lv_obj_t * content = lv_cont_create(par, NULL);
    lv_obj_set_size(content, lv_obj_get_width(par), lv_obj_get_height(par));
    lv_obj_set_style_local_bg_color(content, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_obj_align(content, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    lv_obj_set_click(content, false);

    lv_obj_t * title_label = lv_label_create(content, NULL);
    lv_obj_set_style_local_text_font(title_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_FONT_SUBTITLE);
    lv_label_set_align(title_label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(title_label, false);
    lv_label_set_text_id(title_label, txtId);
    lv_obj_align(title_label, content, LV_ALIGN_IN_TOP_MID, 0, lv_obj_get_height(title_label));
    return content;
}

static lv_obj_t * setting_tab_create_list(lv_obj_t * par)
{
    lv_obj_t * tab_list = lv_list_create(par, NULL);
    lv_obj_set_size(tab_list, lv_obj_get_width(par),
                    lv_obj_get_height(par) - lv_obj_get_height(lv_obj_get_child_back(par, NULL)) * 3);
    lv_obj_align(tab_list, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
    lv_page_set_scrollbar_mode(tab_list, LV_SCROLLBAR_MODE_OFF);
    lv_page_set_edge_flash(tab_list, false);
    lv_obj_set_style_local_bg_color(tab_list, LV_LIST_PART_BG, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_obj_set_style_local_bg_opa(tab_list, LV_LIST_PART_EDGE_FLASH, LV_STATE_DEFAULT, LV_OPA_30);
    lv_obj_set_style_local_pad_bottom(tab_list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT,
                                      lv_obj_get_height(lv_obj_get_child_back(par, NULL)));
    return tab_list;
}

static void setting_tab_create_add_img(lv_obj_t * par)
{
    lv_obj_t * add_img = lv_img_create(par, NULL);
    lv_img_set_src(add_img, ICON_SETTING_TAB_ADD);
    lv_obj_align(add_img, NULL, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_click(add_img, true);
    lv_obj_set_event_cb(add_img, setting_tab_add_event_cb);
    lv_obj_t * add_label = lv_label_create(par, NULL);
    lv_label_set_text_id(add_label, WATCH_TEXT_ID_ADD_TAB);
    lv_obj_align(add_label, add_img, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);
}

static void setting_tab_add_list(lv_obj_t * tab_list)
{
    uint8_t i = 0;
    lv_obj_t * btn;
    if(launcher_tab_get_type(0) == 0) {
        setting_tab_create_add_img(lv_obj_get_parent(tab_list));
    } else {
        for(i = 0; i < WATCH_LAUNCHER_TAB_MAX_COUNT; i++) {
            if(launcher_tab_get_type(i) != 0) {
                btn = setting_tab_list_add(tab_list, launcher_tab_get_type(i), 0);
                lv_watch_set_free_num(btn, i);
            }
        }
        lv_obj_t * add_label = lv_label_create(tab_list, NULL);
        lv_label_set_text_id(add_label, WATCH_TEXT_ID_ADD_TAB);
        lv_obj_align(add_label, tab_list, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
        lv_obj_set_style_local_text_color(add_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_CHECKED);
        lv_obj_set_click(add_label, true);
        lv_obj_set_event_cb(add_label, setting_tab_add_event_cb);

        lv_obj_t * first_btn = lv_list_get_next_btn(tab_list, NULL);
        lv_obj_t * up_img = lv_obj_get_child(first_btn, NULL);
        lv_img_set_src(up_img, ICON_SETTING_TAB_UP_GRAY);
        lv_obj_set_click(up_img, false);
    }
}

static void setting_tab_create_msgbox(lv_obj_t * par)
{
    lv_obj_t * mbox = lv_msgbox_create(par, NULL);
    lv_msgbox_set_text(mbox, (char *)lv_lang_get_text(WATCH_TEXT_ID_NUM_REACHED_MAX));
    lv_obj_align(mbox, par, LV_ALIGN_CENTER, 0, 0);
    lv_msgbox_set_anim_time(mbox, 0);
    lv_msgbox_start_auto_close(mbox, 2000);
}

static void setting_tab_ok_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) return;
    lv_obj_t * btn;
    lv_setting_tab_ext_t * ext = setting_tab_get_ext();
    lv_obj_t * scrl = lv_page_get_scrl(ext->add_list);
    btn = lv_obj_get_child(scrl, obj);
    while(btn) {
        if(lv_btn_get_state(lv_obj_get_child(btn, NULL)) == LV_BTN_STATE_CHECKED_RELEASED) {
            printf("%s:checked btn type = %d\n", __FUNCTION__, lv_watch_get_free_num(btn));
            for(uint8_t i = 0; i < WATCH_LAUNCHER_TAB_MAX_COUNT; i++) {
                if(launcher_tab_get_type(i) == 0) {
                    launcher_tab_set_type(i, lv_watch_get_free_num(btn));
                    break;
                }
            }
        }
        btn = lv_obj_get_child(scrl, btn);
    }
    launcher_tab_save();
    ext->checked_num = 0;
    lv_obj_t * add_img = lv_obj_get_child_back(lv_obj_get_parent(ext->list), ext->list);
    if(add_img) {
        lv_obj_del(lv_obj_get_child_back(lv_obj_get_parent(ext->list), add_img));
        lv_obj_del(add_img);
    }
    lv_list_clean(ext->list);
    setting_tab_add_list(ext->list);
    lv_obj_t * add_obj = lv_watch_get_activity_obj(ACT_ID_SETTING_TAB_ADD);
    setting_tab_add_prepare_destory(add_obj);
    lv_obj_del(add_obj);
}

static void setting_tab_add_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) return;
    lv_setting_tab_ext_t * ext = setting_tab_get_ext();
    if(ext == NULL || ext->list == NULL) return;
    if(lv_list_get_size(ext->list) == WATCH_LAUNCHER_TAB_MAX_COUNT) {
        setting_tab_create_msgbox(lv_obj_get_parent(ext->list));
        return;
    }
    setting_tab_add_create(NULL);
}

static void setting_tab_check_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_VALUE_CHANGED == e) {
        lv_setting_tab_ext_t * ext = setting_tab_get_ext();
        if(lv_btn_get_state(obj) == LV_BTN_STATE_CHECKED_RELEASED) {
            if(launcher_tab_get_cnt() + ext->checked_num == WATCH_LAUNCHER_TAB_MAX_COUNT) {
                setting_tab_create_msgbox(lv_obj_get_parent(ext->add_list));
                lv_btn_set_state(obj, LV_BTN_STATE_RELEASED);
                return;
            }
            ext->checked_num++;

        } else if(lv_btn_get_state(obj) == LV_BTN_STATE_RELEASED) {
            ext->checked_num--;
        }
        printf("%s: check cnt = %d\n", __FUNCTION__, ext->checked_num);
    }
}

static void setting_tab_delete_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) return;
    lv_setting_tab_ext_t * ext = setting_tab_get_ext();
    uint8_t index = lv_watch_get_free_num(lv_obj_get_parent(obj));
    printf("%s:index = %d\n", __FUNCTION__, index);
    for(uint8_t i = 0; i < WATCH_LAUNCHER_TAB_MAX_COUNT; i++) {
        if(launcher_tab_get_type(i) != 0 && i >= index) {
            if(i == WATCH_LAUNCHER_TAB_MAX_COUNT - 1)
                launcher_tab_set_type(i, 0);
            else
                launcher_tab_set_type(i, launcher_tab_get_type(i + 1));
        }
    }
    lv_list_clean(ext->list);
    setting_tab_add_list(ext->list);
    if(index > 1)
        lv_list_focus(setting_tab_list_get_btn(ext->list, index - 1), LV_ANIM_OFF);
}

static void setting_tab_up_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) return;
    uint8_t temp_type = 0;
    lv_setting_tab_ext_t * ext = setting_tab_get_ext();
    uint8_t index = lv_watch_get_free_num(lv_obj_get_parent(obj));
    for(uint8_t i = 0; i < WATCH_LAUNCHER_TAB_MAX_COUNT; i++) {
        if(i == index) {
            temp_type = launcher_tab_get_type(i - 1);
            launcher_tab_set_type(i - 1, launcher_tab_get_type(i));
            launcher_tab_set_type(i, temp_type);
            break;
        }
    }
    lv_list_clean(ext->list);
    setting_tab_add_list(ext->list);
    lv_list_focus(setting_tab_list_get_btn(ext->list, index), LV_ANIM_OFF);
}

static lv_obj_t * setting_tab_list_get_btn(lv_obj_t * list, uint8_t index)
{
    uint8_t i = 0;
    lv_obj_t * e = lv_list_get_next_btn(list, NULL);
    while(e != NULL) {
        if(i == index) {
            return e;
        }
        i++;
        e = lv_list_get_next_btn(list, e);
    }
    return lv_list_get_next_btn(list, NULL);
}

static lv_obj_t * setting_tab_list_add(lv_obj_t * list, uint8_t tab_type, uint8_t choose)
{
    lv_obj_t * btn;
    btn = lv_btn_create(list, NULL);

    /*Save the original signal function because it will be required in `lv_list_btn_signal`*/
    lv_list_set_ancestor_btn_signal(btn);

    /*Set the default styles*/
    lv_obj_set_style_local_bg_opa(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_0);
    lv_obj_set_style_local_bg_opa(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_OPA_0);

    lv_page_glue_obj(btn, true);
    lv_obj_add_protect(btn, LV_PROTECT_PRESS_LOST);
    lv_obj_set_signal_cb(btn, lv_list_get_listbtn_signal());
    lv_coord_t w = lv_obj_get_width(list);
    lv_obj_set_size(btn, w, LV_VER_RES / 3);
    lv_btn_set_layout(btn, LV_LAYOUT_OFF);

    if(choose == 0) {
        lv_obj_t * del_img = lv_img_create(btn, NULL);
        lv_img_set_src(del_img, ICON_SETTING_TAB_DEL);
        lv_obj_set_click(del_img, true);
        lv_obj_set_event_cb(del_img, setting_tab_delete_event_cb);
        lv_obj_align(del_img, btn, LV_ALIGN_IN_LEFT_MID, LV_VER_RES / 9, 0);

        lv_obj_t * label = lv_label_create(btn, NULL);
        lv_label_set_long_mode(label, LV_LABEL_LONG_SROLL_CIRC);
        lv_obj_set_width(label, LV_HOR_RES / 3);
        lv_label_set_text(label, launcher_tab_get_name(tab_type));
        lv_obj_align(label, del_img, LV_ALIGN_OUT_RIGHT_MID, LV_VER_RES / 9, 0);

        lv_obj_t * up_img = lv_img_create(btn, NULL);
        lv_img_set_src(up_img, ICON_SETTING_TAB_UP);
        lv_obj_set_click(up_img, true);
        lv_obj_set_event_cb(up_img, setting_tab_up_event_cb);
        lv_obj_align(up_img, btn, LV_ALIGN_IN_RIGHT_MID, -LV_VER_RES / 9, 0);
    } else {
        lv_obj_t * img = lv_img_create(btn, NULL);
        lv_img_set_src(img, launcher_tab_get_icon(tab_type));
        lv_obj_align(img, btn, LV_ALIGN_IN_LEFT_MID, LV_VER_RES / 9, 0);

        lv_obj_t * label = lv_label_create(btn, NULL);
        lv_label_set_long_mode(label, LV_LABEL_LONG_SROLL_CIRC);
        lv_obj_set_width(label, LV_HOR_RES / 3);
        lv_label_set_text(label, launcher_tab_get_name(tab_type));
        lv_obj_align(label, img, LV_ALIGN_OUT_RIGHT_MID, LV_VER_RES / 9, 0);

        lv_obj_t * cb = lv_btn_create(btn, NULL);
        lv_btn_set_checkable(cb, true);
        lv_obj_add_protect(cb, LV_PROTECT_PRESS_LOST);
        lv_obj_set_width(cb, lv_obj_get_height(cb));
        lv_obj_set_style_local_bg_color(cb, LV_BTN_PART_MAIN, LV_STATE_CHECKED, LV_COLOR_CHECKED);
        lv_obj_set_style_local_outline_opa(cb, LV_BTN_PART_MAIN, LV_STATE_FOCUSED, LV_OPA_TRANSP);
        lv_obj_set_style_local_pattern_image(cb, LV_BTN_PART_MAIN, LV_STATE_CHECKED, LV_SYMBOL_OK);
        lv_obj_set_style_local_pattern_recolor(cb, LV_BTN_PART_MAIN, LV_STATE_CHECKED, LV_COLOR_WHITE);
        lv_obj_set_style_local_text_font(cb, LV_BTN_PART_MAIN, LV_STATE_CHECKED, LV_THEME_WATCH_NIGHT_SYMBOL_NORMAL);
        lv_obj_set_style_local_radius(cb, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_DPX(4));
        lv_obj_set_style_local_pad_left(cb, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_DPX(3));
        lv_obj_set_style_local_pad_right(cb, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_DPX(3));
        lv_obj_set_style_local_pad_top(cb, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_DPX(3));
        lv_obj_set_style_local_pad_bottom(cb, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_DPX(3));
        lv_obj_set_event_cb(cb, setting_tab_check_event_cb);
        lv_obj_align(cb, btn, LV_ALIGN_IN_RIGHT_MID, -LV_VER_RES / 9, 0);
    }
    lv_watch_obj_add_element(btn);
    return btn;
}

static lv_obj_t * setting_tab_create(lv_obj_t * activity_obj)
{
    if(NULL == activity_obj) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_SETTING_TAB;
        activity_ext.create = setting_tab_create;
        activity_ext.prepare_destory = setting_tab_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(NULL == activity_obj) {
            return NULL;
        }
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) {
        return NULL;
    }
    lv_setting_tab_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_setting_tab_ext_t));
    ext->checked_num = 0;

    lv_obj_t * cont = setting_tab_create_title(obj, WATCH_TEXT_ID_SETTING_TAB);
    ext->list = setting_tab_create_list(cont);

    setting_tab_add_list(ext->list);

    lv_watch_obj_add_element(cont);
    lv_watch_obj_add_element(ext->list);
    launcher_tab_update_stop();
    return obj;
}

static lv_obj_t * setting_tab_add_create(lv_obj_t * activity_obj)
{
    if(NULL == activity_obj) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_SETTING_TAB_ADD;
        activity_ext.create = setting_tab_add_create;
        activity_ext.prepare_destory = setting_tab_add_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(NULL == activity_obj) {
            return NULL;
        }
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) {
        return NULL;
    }

    lv_setting_tab_ext_t * ext = setting_tab_get_ext();
    lv_obj_t * cont = setting_tab_create_title(obj, WATCH_TEXT_ID_CHOOSE_TAB);
    ext->add_list = setting_tab_create_list(cont);

    uint8_t i = 0;
    lv_obj_t * btn;
    for(i = TAB_APP_MUSIC; i < TAB_APP_MAX; i++) {
        if(!launcher_tab_is_added(i)) {
            btn = setting_tab_list_add(ext->add_list, i, 1);
            lv_watch_set_free_num(btn, i);
        }
    }

    lv_obj_t * ok_label = lv_label_create(ext->add_list, NULL);
    lv_label_set_text_id(ok_label, WATCH_TEXT_ID_OK);
    lv_obj_align(ok_label, ext->add_list, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
    lv_obj_set_style_local_text_color(ok_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_CHECKED);
    lv_obj_set_click(ok_label, true);
    lv_obj_set_event_cb(ok_label, setting_tab_ok_event_cb);

    lv_watch_obj_add_element(cont);
    lv_watch_obj_add_element(ext->add_list);
    return obj;
}
#endif /*USE_LV_WATCH_LAUNCHER2*/
